package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.system.entity.DictEntity;
import com.sq.universe.system.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dict")
@CrossOrigin
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    /**
    * 查询数据
    * @param dictEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:dict:query")
    public RespEntity query(@RequestBody(required = false) DictEntity dictEntity){
        try {
            return new RespEntity("查询成功",dictService.query(dictEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
     * 查询数据
     * @param dictEntity
     * @return
     */
    @PostMapping("/queryByForm")
//    @RequiresPermissions("system:dict:query")
    public RespEntity queryByForm(DictEntity dictEntity){
        try {
            return new RespEntity("查询成功",dictService.query(dictEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param dictEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:dict:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) DictEntity dictEntity){
        try {
            PageHelper.startPage(dictEntity.getPageNo(),dictEntity.getPageSize());
            List<DictEntity> dictList = dictService.query(dictEntity);
            PageInfo<DictEntity> pageInfo = new PageInfo<DictEntity>(dictList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
     * 查询分组相关数据
     * @param dictEntity
     * @return
     */
    @PostMapping("/queryByGroup")
    @RequiresPermissions("system:dict:queryByGroup")
    public RespEntity queryByGroup(@RequestBody(required = false) DictEntity dictEntity){
        try {
            List<DictEntity> dictList = dictService.queryByGroup(dictEntity);
            return new RespEntity("查询成功",dictList);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param dictEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:dict:insert")
    public RespEntity insert(@RequestBody(required = false) DictEntity dictEntity){
        try {
            dictEntity.setDictCreateTime(new Date());
            dictEntity.setDictCreateUser(this.getCurrentUser().getUserId());
            dictService.insert(dictEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param dictEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:dict:update")
    public RespEntity update(@RequestBody(required = false) DictEntity dictEntity){
        try {
            dictEntity.setDictUpdateTime(new Date());
            dictEntity.setDictUpdateUser(this.getCurrentUser().getUserId());
            dictService.update(dictEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param dictEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:dict:delete")
    public RespEntity delete(@RequestBody(required = false) DictEntity dictEntity){
        try {
            if(dictEntity.getDictId() == null){
                return new RespEntity("删除失败");
            }
            dictEntity.setDictUpdateTime(new Date());
            dictEntity.setDictUpdateUser(this.getCurrentUser().getUserId());
            dictService.logicalDelete(dictEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            return new RespEntity("删除失败");
        }
    }
}